package org.peoples.ui.chat {
	
	import flash.display.Sprite;
	
	import mx.controls.Label;
	import mx.controls.Text;
	import mx.core.UIComponent;
	
	import org.peoples.messages.Message;
	import org.peoples.ui.containers.GradientBar;
	
	public class ChatItem extends GradientBar {
		
		private var _message:Message;
		
		private var mc_seperator:UIComponent;
		private var txt_name:Label;
		private var txt_message:Text;
		
		public function ChatItem() {
			
			super();
			
		}
		
		override protected function createChildren():void {
			super.createChildren();
			
			if (!mc_seperator) {
				mc_seperator = new UIComponent();
				addChild(mc_seperator);
			}
			
			if (!txt_name) {
				txt_name = new Label();
				txt_name.setStyle("textAlign", "right");
				addChild(txt_name);
			}
			
			if (!txt_message) {
				txt_message = new Text();
				txt_message.selectable = true;
				txt_message.text = ".";
				addChild(txt_message);
			}
			
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			
			if (this._message) {
				txt_name.text = this._message.user.firstName + " " + this._message.user.lastName;
				txt_message.htmlText = this._message.message;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			mc_seperator.graphics.clear();
			mc_seperator.graphics.lineStyle(1);
			mc_seperator.graphics.moveTo(100, 0);
			mc_seperator.graphics.lineTo(100, unscaledHeight);
			mc_seperator.graphics.endFill();
			
			txt_name.x = 0;
			txt_name.width = 95;
			
			txt_message.x = 105;
			txt_message.width = unscaledWidth - txt_message.x;
			
			height = txt_message.y + txt_message.height;
		}
		
		public function get message():Message {
			return this._message;
		}
		public function set message(value:Message):void {
			this._message = value;
			
			invalidateProperties();
		}
		
	}
	
}